using Domain.Entities;
using Domain.Entities.Enums;
using Domain.Mail;
using Domain.Services;
using System;
using System.Web.UI.WebControls;

namespace UserInterface.Admin
{
    public partial class ActivateUsers : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
            }
        }

        private void BindData()
        {
            UsersRepeater.DataSource = UserService.GetAwaitingAdminActivation();
            UsersRepeater.DataBind();
        }

        protected void UsersRepeater_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            User user = UserService.GetById(Convert.ToInt32(e.CommandArgument));

            if (e.CommandName == "Activate")
            {
                UserService.UpdateStatus(user, UserStatus.Activated);

                MailParser mailParser = new MailParser(Server.MapPath("~/Emails/AccountActivated.xml"));
                mailParser.AddBodyTag("Username", user.Username);

                MailSender mailSender = new MailSender(mailParser);
                mailSender.AddRecipient(user.Email);
                mailSender.Send();
            }
            else if (e.CommandName == "Deny")
            {
                UserService.UpdateStatus(user, UserStatus.DeniedByAdmin);

                MailParser mailParser = new MailParser(Server.MapPath("~/Emails/AccountDenied.xml"));
                mailParser.AddBodyTag("Username", user.Username);

                MailSender mailSender = new MailSender(mailParser);
                mailSender.AddRecipient(user.Email);
                mailSender.Send();
            }

            BindData();
        }
    }
}
